﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Collections.Generic;
using System.ComponentModel.Composition;

using Caliburn.Micro.BikeWizard.Events;
using Caliburn.Micro.BikeWizard.Services;
using Caliburn.Micro.BikeWizard.Data;
using Caliburn.Micro.BikeWizard.Utils;

namespace Caliburn.Micro.BikeWizard.Views
{
    [Export()]
    public class BikeWizardViewModel : Wizard<Screen>
    {
        #region Private Members
        private BikeOrder bikeOrder;
        #endregion

        #region Public Properties
        public decimal TotalPrice { get { return bikeOrder.Price; } }
        #endregion

        #region Private Properties (Services)
        private IEventAggregator eventAggregator { get; set; }
        private IBikeOptionService optionService { get; set; }
        #endregion

        #region WizardConductor Overrides
        protected override void Finish()
        {
            eventAggregator.Publish<BikeOrderedEvent>(new BikeOrderedEvent(this.bikeOrder));
            base.Finish();
        }
        #endregion

        #region Constructors
        [ImportingConstructor]
        public BikeWizardViewModel(
            IEventAggregator eventAggregator,
            IBikeOptionService optionService)
        {
            this.eventAggregator = eventAggregator;
            this.optionService = optionService;

            this.DisplayName = "Choose your bike";
            this.bikeOrder = new BikeOrder();
            var bikeOrderpropertyChanged = (this.bikeOrder as INotifyPropertyChanged);
            bikeOrderpropertyChanged.PropertyChanged += 
                (sender, e) => 
                {
                    if (e.PropertyName == "Price")
                        NotifyOfPropertyChange(() => TotalPrice);
                };

            var welcomeViewModel = new WelcomeViewModel();
            var bikeTypeFrameSizeViewModel = new BikeTypeFrameSizeViewModel(eventAggregator, optionService, this.bikeOrder);
            var bikeExtrasViewModel = new BikeExtrasViewModel(optionService, this.bikeOrder);
            var bikeSummaryViewModel = new BikeSummaryViewModel(this.bikeOrder);

            base.AddItems(
                welcomeViewModel,
                bikeTypeFrameSizeViewModel,
                bikeExtrasViewModel,
                bikeSummaryViewModel);

            base.ActiveFirstItem();
        }
        #endregion
    }
}
